package com.dge.app.providers;

import com.dge.app.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.List;

public class HotSearchProvider extends RecycleItemProvider {
    private List<String> items;
    private AbilitySlice slice;

    public HotSearchProvider(List<String> dataItemList, AbilitySlice slice) {
        this.items = dataItemList;
        this.slice = slice;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int i) {
        return items.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        Component itemComponent = component;
        TextHolder viewHolder;
        if (itemComponent == null) {
            itemComponent = LayoutScatter.getInstance(slice)
                    .parse(ResourceTable.Layout_item_hot_search, componentContainer, false);
            viewHolder = new TextHolder(itemComponent);
            itemComponent.setTag(viewHolder);
        } else {
            viewHolder = (TextHolder) itemComponent.getTag();
        }
        viewHolder.text.setText(items.get(i));
        return itemComponent;
    }

    public static class TextHolder {
        Text text;

        TextHolder(Component itemComponent) {
            text = (Text) itemComponent.findComponentById(ResourceTable.Id_itemView);
        }
    }
}